package com.yaozheng.vocationaltraining.fragment;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.dialog.ask.CancelAskDialog;
import com.yaozheng.vocationaltraining.dialog.ask.ConfirmCancelAskDialog;

import android.support.v4.app.Fragment;
import android.widget.TextView;

/**
 * baseFragment
 *
 * @author Administrator
 */
public class BaseFragment extends Fragment {
    private ConfirmCancelAskDialog confirmCancelAskDialog;
    private CancelAskDialog cancelAskDialog;


    /**
     * 得到activity
     *
     * @return
     */
    public BaseActivity getBaseActivity() {
        return (BaseActivity) getActivity();
    }

    /**
     * 得到回话签名
     *
     * @return
     */
    public String getToken() {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity == null) return "";
        return getBaseActivity().getToken();
    }

    /**
     * 显示加载数据进度条
     */
    public void showLoadDataProgressDialog() {

    }

    /**
     * 取消加载数据进度条
     */
    public void cancelLoadDataProgressDialog() {

    }

    /**
     * 显示加载数据进度条
     */
    public void showOperatingProgressDialog(String mesage) {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.showProgressDialog(mesage);
        }
    }

    /**
     * 取消操作进度条
     */
    public void cancelOperatingProgressDialog() {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.cancelProgressDialog();
        }
    }

    public boolean isResponseResult() {
        return isAdded();
    }

    /**
     * 显示确认取消对话框
     *
     * @param messageContent 消息内容
     */
    public void showConfirmCancelAskDialog(String messageContent) {
        showConfirmCancelAskDialog(messageContent, "confirmCancelAskDialogDetermineClick");
    }

    /**
     * 显示确认取消对话框
     *
     * @param messageContent 消息内容
     * @param runMethod      运行方法
     */
    public void showConfirmCancelAskDialog(String messageContent, String runMethod) {
        showConfirmCancelAskDialog(messageContent, "请选择", runMethod);
    }

    /**
     * 显示确认取消对话框
     *
     * @param messageContent 消息内容
     * @param title          ==null 隐藏
     * @param runMethod      运行方法
     */
    public void showConfirmCancelAskDialog(String messageContent, String title, String runMethod) {
        if (confirmCancelAskDialog == null) {
            confirmCancelAskDialog = new ConfirmCancelAskDialog(this);
        }
        confirmCancelAskDialog.show();
        confirmCancelAskDialog.setContent(messageContent, title);
        confirmCancelAskDialog.setRunMethod(runMethod);
    }

    /**
     * 显示带确定对话
     *
     * @param messageContent 消息内容
     * @param titileValue    标题
     * @param runMethod      运行方法
     */
    public void showCancelAskDialog(String messageContent, String titileValue, String runMethod) {
        if (cancelAskDialog == null) {
            cancelAskDialog = new CancelAskDialog(this);
        }
        cancelAskDialog.show();
        cancelAskDialog.setContentValue(messageContent);
        cancelAskDialog.setTitileValue(titileValue);
        cancelAskDialog.runMethod(runMethod);
    }

    /**
     * 显示带取消对话框
     *
     * @param messageContent
     * @param runMethod
     */
    public void showCancelAskDialog(String messageContent, String runMethod) {
        showCancelAskDialog(messageContent, "提示", runMethod);
    }

    /**
     * 显示带取消对话框
     *
     * @param messageContent
     */
    public void showCancelAskDialog(String messageContent) {
        showCancelAskDialog(messageContent, "提示", null);
    }

    /**
     * 提示消息
     */
    public void alertMessage(String message) {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.alertMessage(message);
        }
    }

    public void runMethod(String methodName) {
        try {
            getSubClasses().getMethod(String.valueOf(methodName), new Class[0]).invoke(
                    getSubObject(), new Object[]{});
        } catch (Exception e) {
            e.printStackTrace();
            error(e);
        }
    }

    /**
     * token 失效
     */
    public void tokenFail() {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.signOutOperating();
        }
    }

    public void error(Exception e) {
    }

    /**
     * 得到子类class
     *
     * @return
     */
    public Class getSubClasses() {
        return getClass();
    }

    /**
     * 得到子类对象
     *
     * @return
     */
    public Object getSubObject() {
        return this;
    }

    /**
     * true退出
     *
     * @return
     */
    public boolean backPressed() {
        return true;
    }

    @Override
    public void onDestroyView() {
        if (confirmCancelAskDialog != null && confirmCancelAskDialog.isShowing()) {
            confirmCancelAskDialog.cancel();
        }
        if (cancelAskDialog != null && cancelAskDialog.isShowing()) {
            cancelAskDialog.cancel();
        }
        cancelAskDialog = null;
        confirmCancelAskDialog = null;
        super.onDestroyView();
    }
}
